﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Globalization;

namespace GroundStation
{
    public partial class Form1 : Form
    {
        Telemetry tele = new Telemetry();
        ArtificialHorizon ah = new ArtificialHorizon();

        int offsetX = 0;
        int offsetY = 0;
        //bool moving = false;
        Map map = new Map();
        private fdr.SDlogger sdlog;
        private bool isfdr = false;

        public Form1()
        {
            InitializeComponent();
            string[] ports = SerialPort.GetPortNames();

            foreach (string port in ports)
            {
                chooseCOM.Items.Add(port);
                chooseCOM.SelectedItem = port;
            }
            //
            ah.Init(pictureBoxArtificialHorizon);
            map.Init(pbMap);
            map.ZoomIn();
            map.ZoomOut();

            timer.Enabled = true;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //cmd.Stop();
            systemStop();
            
            map.Stop();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;

            ah.Draw(tele.EulPitch, tele.EulRoll, tele.EulHeading, tele.AdcSpeed, tele.AdcAlt);
            map.Update();

            lblSysTime.Text = tele.Time.ToString();
            lblAdcSpeed.Text = tele.AdcSpeed.ToString("0.00", CultureInfo.InvariantCulture);
            lblAdcAlt.Text = tele.AdcAlt.ToString("0.00", CultureInfo.InvariantCulture);
            lblAdcTemp.Text = tele.AdcTemp.ToString("0.00", CultureInfo.InvariantCulture);
            lblVolt1.Text = tele.PwrVolt1.ToString("0.00", CultureInfo.InvariantCulture);
            lblVolt2.Text = tele.PwrVolt2.ToString("0.00", CultureInfo.InvariantCulture);
            lblVolt3.Text = tele.PwrVolt3.ToString("0.00", CultureInfo.InvariantCulture);
            lblVolt4.Text = tele.PwrVolt4.ToString("0.00", CultureInfo.InvariantCulture);
            lblFuel.Text = tele.PwrFuel.ToString("0.00", CultureInfo.InvariantCulture);
            lblRpm.Text = tele.EmsRmp.ToString("0.00", CultureInfo.InvariantCulture);
            lblHeadTemp.Text = tele.EmsTempHead.ToString("0.00", CultureInfo.InvariantCulture);
            lblExhaustTemp.Text = tele.EmsTempExhaust.ToString("0.00", CultureInfo.InvariantCulture);

            map.AddCoord(tele.PosX, tele.PosY);


            if(isfdr)
                if (sdlog.SDIsRunning())
                {
                    if (labelSDFileStatus.Enabled)
                        labelSDFileStatus.Enabled = false;
                    else
                        labelSDFileStatus.Enabled = true;
                }
                else
                {
                    buttonSDFile.Enabled = true;
                    checkSDFile.Enabled = true;
                    comboSDDrive.Enabled = true;
                    labelSDFileStatus.Enabled = true;
                    isfdr = false;
                    //labelSDFileStatus.Text = "File.";
                    checkSDFile.Checked = false;
                }

            timer.Enabled = true;
        }

        private void pbMap_MouseDown(object sender, MouseEventArgs e)
        {
            //moving = true;
            offsetX = e.X;
            offsetY = e.Y;
        }

        private void pbMap_MouseMove(object sender, MouseEventArgs e)
        {
            /*if (moving)
            {
              offsetX -= e.X;
              offsetY -= e.Y;
              map.AddOffset(offsetX, offsetY);
              offsetX = e.X;
              offsetY = e.Y;
            }*/
        }

        private void pbMap_MouseUp(object sender, MouseEventArgs e)
        {
            //moving = false;
        }

        private void buttonZoomIn_Click(object sender, EventArgs e)
        {
            map.ZoomIn();
        }

        private void buttonZoomOut_Click(object sender, EventArgs e)
        {
            map.ZoomOut();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void checkSDFile_CheckedChanged(object sender, EventArgs e)
        {
            if (checkSDFile.Checked)
            {
                string[] drives = Environment.GetLogicalDrives();
                foreach (string drive in drives)
                {
                    System.IO.DriveInfo di = new System.IO.DriveInfo(drive);
                    if (di.DriveType == DriveType.Removable)
                    {
                        try
                        {
                            string dif = di.DriveFormat;
                        }
                        catch (Exception ex)
                        {
                            comboSDDrive.Items.Add(drive);
                            comboSDDrive.SelectedItem = drive;
                            //textLog.AppendText(ex.ToString() + "\r\n");
                        }
                    }
                }
                labelSDFileStatus.Text = "SD Card!";
                comboSDDrive.Visible = true;
            }
            else
            {
                labelSDFileStatus.Text = "File.";
                comboSDDrive.Visible = false;
            }
        }

        private void buttonSDFile_Click(object sender, EventArgs e)
        {
            if (checkSDFile.Checked)
            {
                this.Cursor = Cursors.WaitCursor;
                if (comboSDDrive.SelectedItem == null)
                {
                    //textLog.AppendText(DateTime.Now.ToLongTimeString() + "=> apropriate drive not selected.\r\n");
                }
                else
                {
                    // TODO
                    //textLog.AppendText(DateTime.Now.ToLongTimeString() + "=> using " + logicalDrives.SelectedItem.ToString() + " as RAW source for datamining.\r\n");
                    sdlog = new fdr.SDlogger(comboSDDrive.SelectedItem.ToString(), true);


                    labelSDFileStatus.Text = "Data processing!";
                    buttonSDFile.Enabled = false;
                    checkSDFile.Enabled = false;
                    comboSDDrive.Enabled = false;
                    isfdr = true;
                }
                this.Cursor = Cursors.Default;
            }
            else
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        string file;
                        this.Cursor = Cursors.WaitCursor;
                        if ((file = openFileDialog1.FileName) != null)
                        {
                            //textLog.AppendText(DateTime.Now.ToLongTimeString() + "=> processing SDlogger file " + file + ".\r\n");
                            sdlog = new fdr.SDlogger(file, false);
                            labelSDFileStatus.Text = "Data processing!";
                            checkSDFile.Enabled = false;
                            buttonSDFile.Enabled = false;
                            buttonSDFile.Enabled = false;
                            isfdr = true;
                        }
                        this.Cursor = Cursors.Default;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(DateTime.Now.ToLongTimeString() + "=> error: Could not read file from disk.\r\n\tOriginal error=> " + ex.Message);
                    }
                }
            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttStartStop_Click(object sender, EventArgs e)
        {
            if (buttStartStop.Text.ToString() == "Start")
            {
                tele.Init(chooseCOM.SelectedItem.ToString());
                buttStartStop.Text = "Stop";
                chooseCOM.Enabled = false;
            }
            else
            {
                buttStartStop.Text = "Start";
                chooseCOM.Enabled = true;
            }
        }

        private void systemStop()
        {
            tele.Stop();
        }
    }
}
